//点击菜单显示对应内容
$(() => {
    $('.base-menu').on('click', 'li', function () {
        //清除被选中的表格
        $('.select-tr').removeClass('select-tr');
        //将原本选中的菜单项取消选中
        $('.select').removeClass('select');
        //使点击的菜单项被选中
        $(this).addClass('select');
        //将目前显示的内容块隐藏
        $('.right-contain .show').removeClass('show').addClass('hide');
        //找到相应的内容块，将它显示
        $('.right-contain .hide').each((index, tag) => {
            if ($(this).text() == $(tag).find('.title').text()) {
                $(tag).removeClass('hide').addClass('show');
            }
        });
    });
});

//用户信息
$(() => {
    $('.user-button').text(cookie.find('user')+'('+cookie.find('power')+')');
});

function insertTable (params) {
    let array = [];
    for (let name in params['data'])
        array.push(name+'='+params['data'][name]);

    $.ajax({
        url: params['url'],
        data: array.join('&'),
        dataType: 'json',
        success: params['success'],
        error: (reply) => {
            console.log(reply);
            tip.alert(reply['responseJSON']['message'], params['title'], 'error', 5000);
        }
    });
}

//url, title, view, table, func, data
function initialize(params) {
    let total;
    params['func'] = params['func'] == null ? (reply) => {
    } : params['func'];
    insertTable({
        url: params['url'],
        title: params['title'],
        data: params['data'],
        success: (reply) => {
            let loading = false;
            total = reply['total'];
            let max = Math.ceil(total / params['data']['pageSize']);
            params['func'](reply);
            params['view'].append(tablegen(reply[params['table']], params['titles']));

            //为上一页按钮添加点击事件
            params['view'].parents('.model').find('.chl').unbind('click').click(() => {
                if (!loading) {
                    loading = true;
                    if (params['data']['pageNum'] > 1) {
                        params['data']['pageNum']--;
                        insertTable({
                            url: params['url'],
                            title: params['title'],
                            data: params['data'],
                            success: (reply) => {
                                params['func'](reply);
                                params['view'].append(tablegen(reply[params['table']], params['titles']));
                                loading = false;
                            }
                        });
                    } else {
                        tip.alert('已经是第一页', params['title'], 'warn', 500);
                        loading = false;
                    }
                }
            });

            //为下一页按钮添加点击事件
            params['view'].parents('.model').find('.chr').unbind('click').click(() => {
                //是否正在请求
                if (!loading) {
                    loading = true;
                    if (params['data']['pageNum'] < max) {
                        params['data']['pageNum']++;
                        insertTable({
                            url: params['url'],
                            title: params['title'],
                            data: params['data'],
                            success: (reply) => {
                                params['func'](reply);
                                params['view'].append(tablegen(reply[params['table']], params['titles']));
                                loading = false;
                            }
                        });
                    } else {
                        tip.alert('已经是最后一页', params['title'], 'warn', 500);
                        loading = false;
                    }
                }
            });
        }
    });
}


//url: /pageNum/pageSize/orderNo
function insertTableDetail (params) {
    $.ajax({
        url: params['url'] +'/'+params['number']+'/'+params['size']+'/'+params['order'],
        dataType: 'json',
        success: params['success'],
        error: (reply) => {
            console.log(reply);
            tip.alert(reply['responseJSON']['message'], params['title'], 'error', 5000);
        }
    });
}

//url, title, order, view, table, func
function initializeDetail(params) {
    let number = 1, size = 10, total;
    params['func'] = params['func'] == null ? (reply) => {} : params['func'];
    insertTableDetail({
        url: params['url'],
        title: params['title'],
        order: params['order'],
        number: number,
        size: size,
        success: (reply) => {
            let loading = false;
            total = reply['total'];
            let max = Math.ceil(total / size);
            params['func'](reply);
            params['view'].append(tablegen(reply[params['table']], params['titles']));

            //为上一页按钮添加点击事件
            params['view'].parents('.model').find('.chl').unbind('click').click(() => {
                if (!loading) {
                    loading = true;
                    if (number > 1) {
                        number--;
                        insertTableDetail({
                            url: params['url'],
                            title: params['title'],
                            order: params['order'],
                            number: number,
                            size: size,
                            success: (reply) => {
                                params['func'](reply);
                                params['view'].append(tablegen(reply[params['table']], params['titles']));
                                loading = false;
                            }
                        });
                    } else {
                        tip.alert('已经是第一页', params['title'], 'warn', 500);
                        loading = false;
                    }
                }
            });

            //为下一页按钮添加点击事件
            params['view'].parents('.model').find('.chr').unbind('click').click(() => {
                //是否正在请求
                if (!loading) {
                    loading = true;
                    if (number < max) {
                        number++;
                        insertTableDetail({
                            url: params['url'],
                            title: params['title'],
                            order: params['order'],
                            number: number,
                            size: size,
                            success: (reply) => {
                                params['func'](reply);
                                params['view'].append(tablegen(reply[params['table']], params['titles']));
                                loading = false;
                            }
                        });
                    } else {
                        tip.alert('已经是最后一页', params['title'], 'warn', 500);
                        loading = false;
                    }
                }
            });
        }
    });
}